let http = require('http')
let https = require('https')
let url = require('url')
let fs = require('fs')
let {resolve} = require('path')

let req = http.request('http://www.baidu.com', res => {
    if (res.statusCode == 200 && res.statusCode < 300 || res.statusCode === 304) {
        let arr = []
        res.on('data', (chunk) => {
            arr.push(chunk)
        })
        res.on('end', () => {
            let buffer = Buffer.concat(arr)
            fs.writeFile(resolve('./tmp', 'baidu.html'), buffer, err => {
                if (err) {
                    console.log(err)
                } else {
                    console.log('write file success')
                }
            })
        })
    } else {
        console.log('fail', res.statusCode)
    }
})

req.on('error', () => {
    console.log(error)
})

req.write('')

req.end()